﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary;
using CommonLibrary.Forms;
using System;
using System.Drawing;

namespace BusinessLibrary
{
    /// <summary>
    /// GAGF天平类
    /// </summary>
    public class GAGF : IPlugin
    {
        private string ERROR_WEIGHT = "ERROR";
        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description => "GAGF天平相关业务处理";
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        public string Excute(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            if (pluginParam != null)
            {
                string identity = pluginParam.Identity.ToUpper();
                switch (identity)
                {
                    case "GAGF_WEIGHT": result = GAGFWeight(pluginParam); break;
                    case "GAGF_REZERO": result = GAGFZero(pluginParam); break;
                }
            }
            return result;
        }
        /// <summary>
        /// 返回一个窗口
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>

        public HWFrmWithTitle GetForm(PluginParamEntity pluginParam)
        {
            return null;
        }
        /// <summary>
        /// 返回一个 Bitmap
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBitmap()
        {
            return null;
        }

        /// <summary>
        /// GAGF_WEIGHT 称重
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string GAGFWeight(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            string methodName = "天平称重";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var entity = CommonHelper.GetJson(pluginParam.ParamString, new GAGFExcuteEntity());
                try
                {
                    if (entity != null && entity.WeightPLCEntity != null)
                    {
                        if (!string.IsNullOrWhiteSpace(entity.Weight) && ERROR_WEIGHT != entity.Weight)
                        {
                            entity.Weight = CommonHelper.GetNumberFromString(entity.Weight);
                            float value = float.Parse(entity.Weight);
                            APPInstance.PLC.Write(entity.WeightPLCEntity.ResultAddress, value);
                        }
                        APPInstance.PLC.Write(entity.WeightPLCEntity.ResultDoneAddress, true);
                        result = CommonHelper.GetJsonString(entity, Newtonsoft.Json.Formatting.None);
                        this.Info($"【{pluginParam.Tag}】{methodName} - 写PLC, 地址：{entity.WeightPLCEntity.ResultAddress}, 重量：{entity.Weight} g");
                    }
                }
                catch (Exception ex)
                {
                    this.Error(ex);
                }
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return result;
        }

        /// <summary>
        /// GAGF_REZERO 清零
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string GAGFZero(PluginParamEntity pluginParam)
        {
            string methodName = "天平清零";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var entity = CommonHelper.GetJson(pluginParam.ParamString, new GAGFExcuteEntity());
                try
                {
                    if (entity != null && entity.RezeroPLCEntity != null)
                    {
                        APPInstance.PLC.Write(entity.RezeroPLCEntity.ZeroDoneAddress, true);
                        this.Info($"【{pluginParam.Tag}】{methodName} - 写PLC, 地址：{entity.RezeroPLCEntity.ZeroDoneAddress}");
                    }
                }
                catch (Exception ex)
                {
                    this.Error(ex);
                }
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return string.Empty;
        }
    }
}
